package com.example.service;

import com.example.entity.Employee;
import com.example.exception.CustomException;
import com.example.mapper.EmployeeMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;
    public List<Employee> selectAll(Employee employee) {
        return employeeMapper.selectAll(employee);
    }

    //分页查询
    public PageInfo<Employee> selectPage(Employee employee,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Employee> list = employeeMapper.selectAll(employee);
        return PageInfo.of(list);
    }

    public void add(Employee employee) {
        employeeMapper.insert(employee);
    }

    public void update(Employee employee) {
        employeeMapper.update(employee);
    }

    public void delete(Integer id) {
        employeeMapper.delete(id);
    }

    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids){
            employeeMapper.delete(id);
        }
    }

    public Employee login(Employee employee) {
        String username = employee.getUsername();
        Employee dbemployee = employeeMapper.selectbyusername(username);
        if(dbemployee == null) {
            throw new CustomException("500","账户不存在");
        }
        String password = employee.getPassword();
        if (!dbemployee.getPassword().equals(password)){
            throw new CustomException("500","账号或密码错误");
        }
        return dbemployee;
    }
}
